package com.example.administrator.szb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.adapter.ProDeatilContentAdapter;
import com.example.administrator.szb.adapter.ProDeatilJGAdapter;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.bean.ProjectDetail;
import com.example.administrator.szb.bean.ResultBean;
import com.example.administrator.szb.config.FragmentsFlag;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.SPUtils;
import com.example.administrator.szb.view.MyListView;
import com.example.administrator.szb.xzyw.SuccessActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductDetail2Activity  extends MVPBaseActivity {

    private LinearLayout activity_product_detail;
    public static boolean canRefresh = false;
    private TextView tv_top_status;
    private LinearLayout jg_ll,tel_ll;

    private LinearLayout look_more_ll;
    private MyListView listView1;
    private MyListView listView2;
    private LinearLayout status_ll;
    private TextView project_status;

    private TextView user_name;
    private TextView user_phone;
    private TextView other_msg,tv_pro_status_title;
    private TextView pro_number;
    private TextView pro_time;
    private LinearLayout footer_ll;
    private TextView tv_jujue;
    private TextView tv_apply;
    private ProDeatilJGAdapter adapter2;
    private List<ProjectDetail.YWInfoData> list = new ArrayList<>();
    private List<ProjectDetail.ServiceCounBean> list2 = new ArrayList<>();
    private ProjectDetail projectDetail;
    private ProDeatilContentAdapter adapter1;
    private LinearLayout ll_content;
    private ScrollView scrollView2;

    int dd_id;//订单id
    int genre;//1,我申请，2，申请我的
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);
        canRefresh = false;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        tel_ll = findViewById(R.id.tel_ll);
        tel_ll.setVisibility(View.GONE);
        scrollView2 = findViewById(R.id.scrollView2);
        TextView bar_title = findViewById(R.id.bar_title);
        bar_title.setText("项目详情");
        ll_content = findViewById(R.id.ll_content);
        activity_product_detail = (LinearLayout) findViewById(R.id.activity_product_detail);
        tv_top_status = (TextView) findViewById(R.id.tv_top_status);
        jg_ll = (LinearLayout) findViewById(R.id.jg_ll);
        look_more_ll = (LinearLayout) findViewById(R.id.look_more_ll);
        listView1 = (MyListView) findViewById(R.id.listView1);
        listView2 = (MyListView) findViewById(R.id.listView2);
        status_ll = (LinearLayout) findViewById(R.id.status_ll);
        project_status = (TextView) findViewById(R.id.project_status);
        user_name = (TextView) findViewById(R.id.user_name);
        user_phone = (TextView) findViewById(R.id.user_phone);
        other_msg = (TextView) findViewById(R.id.other_msg);
        pro_number = (TextView) findViewById(R.id.pro_number);
        pro_time = (TextView) findViewById(R.id.pro_time);
        footer_ll = (LinearLayout) findViewById(R.id.footer_ll);
        tv_jujue = (TextView) findViewById(R.id.tv_jujue);
        tv_apply = (TextView) findViewById(R.id.tv_apply);
        tv_pro_status_title = findViewById(R.id.tv_pro_status_title);


    }

    @Override
    protected void initEvent() {

        look_more_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,FWJGActivity.class);
                intent.putExtra("datas",JSON.toJSONString(projectDetail.getServiceCoun()));
                startActivity(intent);
            }
        });
        tv_jujue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, YJFKActivity.class);
                intent.putExtra("who", FragmentsFlag.Activity_XMXQ_JJJD.ordinal());
                intent.putExtra("id_dd",projectDetail.getId());
//                /**
//                 * todo id_dd为订单号
//                 */
//                intent.putExtra("id_dd", 0);
                startActivity(intent);
            }
        });
        tv_apply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestLJJD();
            }
        });
    }




    private void requestLJJD() {
        HashMap<String,String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("indent_id",dd_id+"");
        map.put("users_id", SampleApplicationLike.getUserloginInstance().getId()+"");
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, ResultBean.class, URLAddress.URLBASE + URLAddress.URL_LJJD, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                ResultBean bean = (ResultBean) response;
                if (bean.getResult() == 1){
                    canRefresh = true;
                    Intent intent = new Intent(context, SuccessActivity.class);
                    intent.putExtra("who",FragmentsFlag.XMXQActivity.ordinal());
                    startActivity(intent);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

    }


    @Override
    protected void initData() {

        dd_id = getIntent().getIntExtra("dd_id", -1);
        genre = getIntent().getIntExtra("genre",-1);
        adapter2 = new ProDeatilJGAdapter(list2,context);
        listView2.setAdapter(adapter2);
        adapter1 = new ProDeatilContentAdapter(list,context);
        listView1.setAdapter(adapter1);
        requestDeatil();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (canRefresh){
            canRefresh = false;
            requestDeatil();
        }
    }

    private void initViews(){
        footer_ll.setVisibility(View.GONE);
        if (projectDetail.getStatusType()==1){
            footer_ll.setVisibility(View.VISIBLE);
        }
        String str = "项目状态";
        status_ll.setVisibility(View.GONE);
        switch (projectDetail.getStatusType()){
            case 4:
                str="拒单";
                status_ll.setVisibility(View.VISIBLE);
                break;
            case 8:
                str="项目失败";
                status_ll.setVisibility(View.VISIBLE);
                break;
        }
        if (list.size()>0){
            ll_content.setVisibility(View.VISIBLE);
        }
        if (list2.size()>0){
            //jg_ll.setVisibility(View.VISIBLE);
        }
        tv_top_status.setText("项目状态："+projectDetail.getStatus());
        tv_pro_status_title.setText(str+"原因");
        project_status.setText(projectDetail.getReasons());
        user_name.setText(projectDetail.getName());
        user_phone.setText(projectDetail.getTel());
        other_msg.setText(projectDetail.getContent());
        pro_number.setText(projectDetail.getOrder_number());
        pro_time.setText(projectDetail.getCreate_time());

        adapter1.notifyDataSetChanged();
        adapter2.notifyDataSetChanged();
        scrollView2.smoothScrollTo(0,0);
    }

    private void requestDeatil(){
        Map<String,Object> params = new HashMap<>();
        params.put("users_id", SPUtils.getUserId());
        params.put("genre",genre);
        params.put("indent_id",dd_id);
        params.put("code", HttpUtil.getParam());
        HttpUtils.post(activity,URLAddress.URLBASE+URLAddress.URL_DDXQ, params,  new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {

                try {
                    projectDetail = JSON.parseObject(str,new TypeReference<ProjectDetail>(){});
                } catch (Exception e) {

                    str = str.replace("\"indentInfo\":[]","\"indentInfo\":null");
                    projectDetail = JSON.parseObject(str,new TypeReference<ProjectDetail>(){});
                }

                list2.clear();

                if (projectDetail.getServiceCoun()==null)
                {
                    projectDetail.setServiceCoun(new ArrayList<ProjectDetail.ServiceCounBean>());
                }
                list2.addAll(projectDetail.getServiceCoun());
                list.clear();
                ProjectDetail.YWInfoData infoData = new ProjectDetail.YWInfoData();
                infoData.setKeyName("业务类型");
                infoData.setValueName(projectDetail.getBusinessName());
                list.add(infoData);
                if(projectDetail.getIndentInfo()!=null){

                    for (Map.Entry<Integer, ProjectDetail.YWInfoData> entry : projectDetail.getIndentInfo().entrySet()) {
                        //Map.entry<Integer,String> 映射项（键-值对）  有几个方法：用上面的名字entry
                        //entry.getKey() ;entry.getValue(); entry.setValue();
                        //map.entrySet()  返回此映射中包含的映射关系的 Set视图。
                        list.add(entry.getValue());
                    }

                }
                initViews();
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }
}
